package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.ParentOrder;
import com.baomidou.ant.entity.ProcessCondition;
import com.baomidou.ant.entity.WorkOrder;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private IWorkOrderService workOrderService;
    @Autowired
    private IReportOrderService reportOrderService;
    @Autowired
    private IParentOrderService parentOrderService;
    @Autowired
    private IProcessConditionService processConditionService;

    //先检查是否有模板
    @GetMapping("/checkMould/{orderId}")
    public Result checkMould(@PathVariable Integer orderId){
        WorkOrder workOrder=workOrderService.getById(orderId);
        ParentOrder parentOrder=parentOrderService.getById(workOrder.getParentId());
        //找到当前工单当前产品正在使用的模板
        QueryWrapper<ProcessCondition> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pin_id",parentOrder.getPinId());
        queryWrapper.eq("mould_status",1);
        //判断是否找到.判断是否列印过
        if(workOrder.isDown()){
            return Result.getFailure();
        }
        if(processConditionService.list(queryWrapper)==null||processConditionService.list(queryWrapper).size()==0){
            return Result.getFailure();
        }else
            return Result.getSuccess();
    }

    //导出数据处理接口
    @GetMapping("/handleData/{data}")
    public Result handleData(@PathVariable Integer[] data){
        //定义工单号集合，筛选父工单种类
        List<WorkOrder> workOrderList=new ArrayList<>();
        Set<Integer> parentIdSet=new HashSet<>();
        //存放list里好取值
        List<Integer> parentIdList=new ArrayList<>();
        for(int i:data){
            WorkOrder workOrder=workOrderService.getById(i);
            workOrderList.add(workOrder);
            ParentOrder parentOrder=parentOrderService.getById(workOrder.getParentId());
            parentIdSet.add(parentOrder.getId());
        }
        //得到父工单种类
        for(int i:parentIdSet){
            parentIdList.add(i);
        }
        //定义存放list
        List<String> allOrderList=new ArrayList<>();
        //按照父工单分开存放子工单数量
        for(int i=0;i<parentIdList.size();i++){
            String str = "";
            //循环一轮，分开存放
            for(int j=0;j<workOrderList.size();j++){
                if(Objects.equals(workOrderList.get(j).getParentId(), parentIdList.get(i))){
                    str=MyStringUtil.strToString(String.valueOf(workOrderList.get(j).getId()),str);
                }
            }
            allOrderList.add(str);
        }
        //处理存放list
        List<String> resultList=new ArrayList<>();
        for(String str:allOrderList){
            String[] strings=str.split(";");
            for(String s:strings){
                resultList.add(s);
            }
            //奇数补足,以便于两两配对，防止错误,保证list是偶数个
            if(strings.length%2!=0) {
                resultList.add("0");
            }
        }
        //结果集处理
        String[] resultArr=new String[(resultList.size()/2)];
        for(int k=0;k<resultList.size();k=k+2){
            resultArr[k/2]=resultList.get(k)+","+resultList.get(k+1);
        }
        return Result.getSuccess().setData(resultArr);
    }

    //导出接口
    @GetMapping("/exportTemplateData/{orderIds}")
    public void exportTemplateData(@PathVariable String orderIds,HttpServletResponse response){
//        int[] ids=MyStringUtil.toInt(orderIds);
//        List<WorkOrder> workOrderList=new ArrayList<>();
//        for(int i:ids){
//            //为0忽略，不为0则录入
//            if(i!=0){
//                workOrderList.add(workOrderService.getById(i));
//            }
//        }
//        workOrderService.exportTemplateProvinceAreaData(response,workOrderList);
//        //改变下载状态
//        for(WorkOrder workOrder:workOrderList){
//            workOrder.setDown(true);
//            workOrderService.updateById(workOrder);
//        }
        workOrderService.exportTemplateProvinceAreaData(response,orderIds);
        //改变下载状态
        WorkOrder workOrder=workOrderService.getById(orderIds);
        workOrder.setDown(true);
        workOrderService.updateById(workOrder);
    }

//    @GetMapping("/exportTemplateData/{orderIds}")
//    public void exportTemplateData(@PathVariable Integer[] orderIds,HttpServletResponse response){
//        WorkOrder workOrder=workOrderService.getById(orderId);
//        workOrderService.exportTemplateProvinceAreaData(response,workOrder.getOrderNum());
//        //改变下载状态
//        workOrder.setDown(true);
//        workOrderService.updateById(workOrder);
//    }

    @GetMapping("/exportReportInfo/{vo}")
    public void exportReportInfo(@PathVariable String[] vo,HttpServletResponse response){
        reportOrderService.exportReportInfo(response,vo);
    }

    @GetMapping("/exportRetentionInfo/{vo}")
    public void exportRetentionInfo(@PathVariable String[] vo,HttpServletResponse response){
        workOrderService.exportRetentionInfo(response,vo);
    }
}
